package com.leyou.common.advice;

import com.leyou.common.exceptions.LyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;


@ControllerAdvice//默认情况下，会拦截所有加了@Controller的类
public class CommonExceptionAdvice {
    /**
     * 统一异常处理器
     * 通过@ExceptionHandler(LyException.class)指定要拦截的异常的类型，这里是自定义的异常
     */
    @ExceptionHandler(LyException.class)//拦截自定义异常
    public ResponseEntity<String> handleException(LyException e){
        // 返回自定义信息ResponseEntity
        return ResponseEntity.status(e.getStatus()).body(e.getMessage());
    }
}